﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.Entities;
using Ciripescu_WebRole.BL;
using Microsoft.Samples.ServiceHosting.StorageClient;

namespace Ciripescu_WebRole
{
    public partial class settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            divPictureErrors.InnerHtml = String.Empty;
            divPassErrors.InnerHtml    = String.Empty;
            divNoticesError.InnerHtml  = String.Empty;

            if (!IsPostBack)
                PopulateTabContainer();
           
        }

        private void PopulateAccountTab()
        {
            User currUsr     = (new UserManager()).GetItem((string)Session["UserName"]);
            txtRealName.Text = currUsr.FullName;
            txtEmail.Text    = currUsr.Email;
            
            txtInfoURL.Text  = currUsr.InfoURL    != String.Empty ? currUsr.InfoURL    : String.Empty; 
            txtOneBio.Text   = currUsr.OneLineBio != String.Empty ? currUsr.OneLineBio : String.Empty;
            txtHi5.Text      = currUsr.Hi5URL     != String.Empty ? currUsr.Hi5URL     : String.Empty;
            txtTagged.Text   = currUsr.TaggedURL  != String.Empty ? currUsr.TaggedURL  : String.Empty;
            txtLocation.Text = currUsr.Location   != String.Empty ? currUsr.Location   : String.Empty;
        }

        private void PopulateTabContainer()
        {
            //if (TabContainer1.ActiveTabIndex == 0)
                PopulateAccountTab();

            //if (TabContainer1.ActiveTabIndex == 2)
            {
                User currUsr     = (new UserManager()).GetItem((string)Session["UserName"]);
                chkMailWhenFollow.Checked    = currUsr.EmailWhenFollowMe;
                chkMailDirectMessage.Checked = currUsr.EmailWhenDirectMsg; 
            }

            //if (TabContainer1.ActiveTabIndex == 3)
            {
               
                UserManager uManag = new UserManager();
                User currUser      = uManag.GetItem((string)Session["UserName"]);           
               imgPicture.ImageUrl = currUser.PictureUri; 
            }
        }
        protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
        {
            PopulateTabContainer();
        }

        protected void btnSaveProfile_Click(object sender, ImageClickEventArgs e)
        {
            UserManager uManag = new UserManager(); 
            User currUsr       = uManag.GetItem((string)Session["UserName"]);
            currUsr.FullName   = txtRealName.Text;
            currUsr.Email      = txtEmail.Text;
            currUsr.InfoURL    = txtInfoURL.Text;
            currUsr.OneLineBio = txtOneBio.Text;
            currUsr.Hi5URL     = txtHi5.Text;
            currUsr.TaggedURL  = txtTagged.Text;
            currUsr.Location   = txtLocation.Text;

            uManag.Save(currUsr);
        }

        protected void btnChangePass_Click(object sender, ImageClickEventArgs e)
        {
            UserManager uManag = new UserManager();
            User currUsr       = uManag.GetItem((string)Session["UserName"]);

            if (Utils.MD5Crypt(txtCurrPass.Text).CompareTo(currUsr.Password) == 0)
                if (txtNewPass1.Text.CompareTo(txtNewPass2.Text) == 0)
                {
                    currUsr.Password = Utils.MD5Crypt(txtNewPass1.Text);
                    uManag.Save(currUsr);
                }
               else
                    divPassErrors.InnerHtml = Properties.Resources.ErrNoPassMatch;
            else
               divPassErrors.InnerHtml = Properties.Resources.ErrWrongPass;
        }

        protected void btnSaveNotices_Click(object sender, ImageClickEventArgs e)
        {
            UserManager uManag = new UserManager();
            User currUsr       = uManag.GetItem((string)Session["UserName"]);

            currUsr.EmailWhenDirectMsg = chkMailDirectMessage.Checked;
            currUsr.EmailWhenFollowMe  = chkMailWhenFollow.Checked;

            uManag.Save(currUsr);
        }

        protected void btnSavePicture_Click(object sender, ImageClickEventArgs e)
        {
            if ((updImage.FileName != String.Empty) &&
               (updImage.PostedFile.ContentType == "image/jpeg" ||
                updImage.PostedFile.ContentType == "image/jpg" ||
                updImage.PostedFile.ContentType == "image/gif" ||
                updImage.PostedFile.ContentType == "image/png" ||
                updImage.PostedFile.ContentType == "image/bmp" ||
                updImage.PostedFile.ContentType == "image/pjpeg" ||
                updImage.PostedFile.ContentType == "image/pjpg" ||
                updImage.PostedFile.ContentType == "image/pgif" ||
                updImage.PostedFile.ContentType == "image/ppng" ||
                updImage.PostedFile.ContentType == "image/pbmp" ||
                updImage.PostedFile.ContentType == "image/x-png")
               )

                if (updImage.PostedFile.ContentLength <= 700000) //(KB)
                {
                    CBlobStorage cb    = new CBlobStorage();
                    UserManager uManag = new UserManager();
                    User currUser      =  uManag.GetItem((string)Session["UserName"]);


                    bool needToUpdate = false;
                    if (currUser.PictureBlobId == String.Empty)
                    {
                        currUser.PictureBlobId = Guid.NewGuid().ToString();
                        needToUpdate = true;
                    }
                    byte[] newImg;
                    newImg = ImageResize.ResizeFromByteArray(48, updImage.FileBytes, updImage.PostedFile.FileName);

                    cb.SaveImage(currUser.PictureBlobId, updImage.PostedFile.ContentType.ToString(), newImg);                   
                    string pictureUri   = cb.GetImage(currUser.PictureBlobId).Uri.ToString();
                    imgPicture.ImageUrl = pictureUri;

                   // if (needToUpdate)
                    //{
                        currUser.PictureUri = pictureUri;
                        uManag.Save(currUser);
                   // }
                }
                else
                    divPictureErrors.InnerHtml = Properties.Resources.ErrFileSize;
            else
                divPictureErrors.InnerHtml = Properties.Resources.ErrFileType;
        }

        protected void lnkForgetPass_Click(object sender, EventArgs e)
        {
            Response.Redirect("/resetpass/");
        }

        

            }
}
